﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace JEdwonTune.Lib.Jskit {
    public class JsParser {
        public static String ParseJsFile2Zipped(String fileName, String filePath) {
            String[] lines = File.ReadAllLines(filePath);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < lines.Length; i++) {
                String tmp = lines[i];
                //去掉首尾空格
                tmp = tmp.Trim();
                tmp = tmp.Replace("\t", "");
                //去掉单行的注释
                if (tmp.IndexOf("//") == 0) continue;

                String pattern = "[;|{|}|(|)|\\]|\\[]*\\s*(//.*$)";
                Match match = Regex.Match(tmp, pattern);
                if (match != null && match.Groups.Count == 3) {
                    tmp = tmp.Replace(match.Groups[2].Value, "");
                }

                //去掉换行回车
                tmp = tmp.Replace("\r\n", "");
                tmp = tmp.Replace("\r", "");
                tmp = tmp.Replace("\n", "");
                sb.Append(tmp);
            }
            String content = sb.ToString();
            //将*/换成特别字符
            content = content.Replace("*/", "`");
            while (Regex.IsMatch(content, "/\\*[^`]*`")) {
                content = Regex.Replace(content, "/\\*[^`]*`", "");
            }
            //去掉等号两边的空格
            content = Regex.Replace(content, "[ ]*=[ ]*", "=");
            //去掉加号两边的空格
            content = Regex.Replace(content, "[ ]*\\+[ ]*", "+");
            //去掉冒号两边的空格
            content = Regex.Replace(content, "[ ]*:[ ]*", ":");
            return content;
        }
    }
}
